package com.ym.medical.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.core.utils.tkmybatis.BaseSVImpl;

import com.ym.medical.dao.NavDestHisDao;
import com.ym.medical.domain.entity.NavDestHisEntity;
import com.ym.medical.domain.entity.NavDestYjEntity;
import com.ym.medical.domain.param.NavDestHisParam;
import com.ym.medical.domain.vo.NavDestHisVo;
import com.ym.medical.service.INavDestHisSV;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ComputerName:LAPTOP-I6GKDIFQ
 * Author:admin
 * Date: 2019-06-17
 * Time: 09:23:56
 * 服务实现类
 */
@Service
@Transactional( rollbackFor = Exception.class)
public class NavDestHisSVImpl extends BaseSVImpl<NavDestHisEntity, NavDestHisDao> implements INavDestHisSV {

    @Override
    public PageInfo<NavDestHisVo> listHISNavigateData(NavDestHisParam navDestHisModel, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<NavDestHisVo> navDestHisModels = mapper.listHISNavigateData(navDestHisModel);
        return new PageInfo<NavDestHisVo>(navDestHisModels);
    }

    @Override
    public PageInfo<NavDestYjEntity> selectYjDataByHisHosCode(String hosCode, String deptName, String deptType, int pageNo, int limit) {
        PageHelper.startPage(pageNo,limit);
        List<NavDestYjEntity> navDestYjs =  mapper.selectYjDataByHisHosCode(hosCode,deptName,deptType);
        return new PageInfo<>(navDestYjs);
    }

    @Override
    public Map<String, Object> uploadHisDeptInfo(List<NavDestHisEntity> navDestHisList,Long userId) {
        HashMap<String, Object> result = new HashMap<>();
        ArrayList<Object> failList = new ArrayList<>();
        Integer rows = 0;
        List<NavDestHisEntity> list;
        for (NavDestHisEntity navDestHis : navDestHisList) {
            try {
                NavDestHisEntity entity = NavDestHisEntity.builder().hosCode(navDestHis.getHosCode())
                        .deptCode(navDestHis.getDeptCode())
                        .deptType(navDestHis.getDeptType())
                        .roomCode(navDestHis.getRoomCode()).build();
                list =  mapper.select(entity);
                if (!list.isEmpty()) {//修改
                    System.out.println("------------------------------" + navDestHis.getDeptCode());
                    navDestHis.setModifier(userId);
                    mapper.updateByExampleSelective(navDestHis, entity);
                } else {//新增
                    navDestHis.setCreator(userId);
                    mapper.insertSelective(navDestHis);
                }
                rows++;
            }catch (Exception e){
                failList.add(navDestHis);
            }
        }

        result.put("successNum",rows);
        result.put("failNum",rows);
        result.put("fail",failList);
        return result;
    }
}
